import { createSlice } from "@reduxjs/toolkit";
import { request,getToken,setItem } from '@/utils/index.js'
const token = getToken()
const user = createSlice({
    name: 'user',
    initialState:{
        token:token ? token: ''
    },
    reducers:{
        setToken(state,action){
            state.token = action.payload
            setItem(action.payload)
        }
    }
})
const { setToken } = user.actions
const fetLogin = (loginFrom) => {
    return async (dispatch) => {
        const res = await request.post('/authorizations',loginFrom)
        console.log(res.data.data.token)
        dispatch(setToken(res.data.data.token))
    }
}
export { setToken,fetLogin }
const userRender = user.reducer
export default userRender